# -*- coding:utf-8 -*-
"""
	20-11-2009. Coet @ cawiki
	Classes:
		Site
		Browser
"""

import codecs, getpass, HTMLParser, httplib, json, os, socket, urllib, urllib2
from datetime import datetime, timedelta
from urlparse import urlparse
from functions import encode, urlencode, is_ip

def unzip(data):
	from cStringIO import StringIO
	import gzip

	return gzip.GzipFile(fileobj = StringIO(data)).read()

_Groom = urllib2.build_opener()
class _Surfer:
	def __init__(self, debug=False):
		self._debug = debug

		self._protocol = "http"
		self._sitename = ""
		self._path = ""
		self._params = ""
		self._query = ""
		self._fragment = ""

		self._content_type = "application/x-www-form-urlencoded"
		self._accept_enc = "gzip"
		self._user_agent = "Mozilla/5.0 (Windows NT 5.1; rv:1.0) Hermex/1.0 Geni/2.0"
		self._headers = {}
		self._cookies = None

		self.set_headers()

	def set_headers(self, update=None):
		self._headers = {
			"Content-Type": self._content_type,
			"Accept-Encoding": self._accept_enc,
			"User-Agent": self._user_agent
		}
		if update and isinstance(update, dict):
			u = dict([(k.title(), v) for k, v in update.iteritems()])
			self._headers.update(u)

	def set_protocol(self, protocol):
		self._protocol = "%s://" % protocol

	def set_sitename(self, site):
		self._sitename = site

	def make_url(self, url=None):
		if url:
			if not "://" in url: url = "http://%s" % url
			url = urllib.unquote(encode(url))
			url = urlparse(url)
			self._protocol = url.scheme
			self._sitename = url.netloc
			self._path = url.path
			self._params = url.params
			self._query = url.query and ("?%s" % url.query)
			self._fragment = url.fragment and ("#%s" % fragment)
		self._url = "".join(
			(
				self._protocol and "%s://" % self._protocol,
				self._sitename,
				self._path,
				self._params,
				self._query and "?%s" % self._query,
				self._fragment and "#%s" % self._fragment
			)
		)

	def get_ident(self, user="", passw=True):
		try:
			import cfg
		except ImportError:
			import sys
			sys.path.append(os.path.split(os.getcwd())[0])
			import cfg
		if hasattr(cfg, "netuser"):
			self._user = user
		else:
			self._user = raw_input("User:")
		self._password = ""
		if passw:
			while self._password:
				self._password = getpass.getpass("Password:")
		return self._user, self._password


	def connection(self, params):
		if isinstance(params, dict): params = urlencode(params)
		#print __name__, "Surfer.connection", self._url, params, self._headers
		self._headers["Content-Length"] = str(len(params))
		request = urllib2.Request(self._url, params, self._headers)
		response = _Groom.open(request)
		headers, data = response.info(), response.read()
		if headers.get("content-encoding") == "gzip":
			data = unzip(data)
		return headers, data

	def get_cookies(self):
		logindata = os.path.join(os.getcwd(), "login-data")
		file = "%s_%s.data" % (prefix, self._user)
		file = os.path.join(logindata, file)

		if os.path.exists(file):
			self._cookies = open(file, "r").read()
			return
		else:
			print "no cookies for %s" % self._sitename
			return ""

	def save_cookies(self, data):
		logindata = os.path.join(os.getcwd(), "login-data")
		if not os.path.exists(logindata):
			os.makedirs(logindata)

		file = "%s_%s.data" % (data['cookieprefix'], data['lgusername'])
		file = os.path.join(logindata, file)

		if not os.path.exists(file):
			f = open(file, "w")
			f.wite(data)
			f.close()

	def get_url(self, url, headers=None, time_out=None):
		if headers: self.set_headers(headers)
		if time_out: socket.setdefaulttimeout(float(time_out))
		url= urllib.unquote(encode(url))
		url = urlparse(url)

		self._sitename = url.netloc
		self._path = url.path
		query = url.query.split("&")
		params={}
		if query != ['']:
			for e in query:
				k, v = e.split("=")
				params[k]=v
			if "private" in url:self._query = query
		elif len(self._path.split("/")[-1])>0:
			#experimental. Doesn't work when url is "http://serv.dom.com/path/param"
			params = self._path.split("/")[-1]
			#self._path = "/".join(self._path.split("/")[:-1])
		self.make_url()
		data = self.connection(params)
		return data

	def get_request(self, url, headers=None, time_out=None):
		if headers: self.set_headers(headers)
		if time_out: socket.setdefaulttimeout(float(time_out))
		req = urllib2.Request(url, headers=self._headers)
		response = urllib2.urlopen(req)
		if response.info().get("content-encoding") == "gzip":
			return response.info(), unzip(response.read())
		return response.info(), response.read()

Xplorer = _Surfer()
